package model

import (
	"context"
	"time"

	"code.bydev.io/cht/customer/kyc_admin_service/internal/utils"
	"code.bydev.io/frameworks/byone/core/trace"
	"emperror.dev/errors"
	"go.opentelemetry.io/otel"
	"gorm.io/gorm"
)

const TableNameKycActionImage = "kyc_action_image"

// KycActionImage mapped from table <kyc_action_image>
type KycActionImage struct {
	ID           int64     `gorm:"column:id;type:bigint(20) unsigned;primaryKey;autoIncrement:true;comment:ID" json:"id"`                                                                                      // ID
	MemberID     int64     `gorm:"column:member_id;type:bigint(20);not null;comment:用户ID" json:"member_id"`                                                                                                    // 用户ID
	Provider     int32     `gorm:"column:provider;type:tinyint(4);not null;index:idx_img_id_provider,priority:1;index:idx_provider_img_id,priority:1;comment:认证通道(1:sumsub,2:jumio;3:onfido)" json:"provider"` // 认证通道(1:sumsub,2:jumio;3:onfido)
	IdempotentID string    `gorm:"column:idempotent_id;type:varchar(64);not null;index:idx_idempotent_id,priority:1;comment:认证流水唯一ID" json:"idempotent_id"`                                                    // 认证流水唯一ID
	AccountID    string    `gorm:"column:account_id;type:varchar(64);not null;comment:applicant ID" json:"account_id"`                                                                                         // applicant ID
	RecordID     string    `gorm:"column:record_id;type:varchar(64);not null;comment:data ID" json:"record_id"`                                                                                                // data ID
	ImgID        string    `gorm:"column:img_id;type:varchar(128);not null;index:idx_img_id_provider,priority:2;index:idx_provider_img_id,priority:2;comment:图片资源ID(通道)" json:"img_id"`                        // 图片资源ID(通道)
	ImgType      int32     `gorm:"column:img_type;type:tinyint(4);not null;comment:图片类型(0:证件正面;1:证件背面;2:人脸;3:活体)" json:"img_type"`                                                                             // 图片类型(0:证件正面;1:证件背面;2:人脸;3:活体)
	ImgStatus    int32     `gorm:"column:img_status;type:tinyint(4);not null;comment:图片处理状态(0:init;1:complete)" json:"img_status"`                                                                             // 图片处理状态(0:init;1:complete)
	RetryTimes   int32     `gorm:"column:retry_times;type:int(11);not null;comment:图片存储信息" json:"retry_times"`                                                                                                 // 图片存储信息
	CreatedAt    time.Time `gorm:"column:created_at;type:datetime;not null;index:idx_created_at,priority:1;default:CURRENT_TIMESTAMP;comment:记录创建时间" json:"created_at"`                                        // 记录创建时间
	UpdatedAt    time.Time `gorm:"column:updated_at;type:datetime;not null;default:CURRENT_TIMESTAMP;comment:记录更新时间" json:"updated_at"`                                                                        // 记录更新时间
}

// TableName KycActionImage's table name
func (*KycActionImage) TableName() string {
	return TableNameKycActionImage
}

// GetListByIdempotentID get list by idempotent_id
func (i *KycActionImage) GetListByIdempotentID(ctx context.Context, idempotentID string) ([]*KycActionImage, error) {
	ctx, span := otel.Tracer(trace.TracerName).Start(ctx, utils.GetPFuncName())
	defer span.End()

	db := DBKyc(ctx)
	img := make([]*KycActionImage, 0)
	err := db.
		Where("idempotent_id = ?", idempotentID).
		Find(&img).Error
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return img, nil
		}
	}

	return img, nil
}

// GetCountByIdempotentID returns the count of the given id in the given database
func (i *KycActionImage) GetCountByIdempotentID(ctx context.Context, idempotentID string) (int64, error) {
	ctx, span := otel.Tracer(trace.TracerName).Start(ctx, utils.GetPFuncName())
	defer span.End()

	db := DBKyc(ctx)
	var count int64
	err := db.
		Model(&KycActionImage{}).
		Where("idempotent_id = ?", idempotentID).
		Count(&count).Error
	if err != nil {
		return 0, err
	}

	return count, nil
}
